- Notifications
You must be signed in to change notification settings - Fork 9.1k
/
Copy pathSolution.swift
35 lines (31 loc) · 803 Bytes
/
Solution.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/** public class ListNode {
* var val: Int
* var next: ListNode?
* init(_ x: Int) {
* self.val = x
* self.next = nil
* }
* }
*/
classSolution{
func partition(_ head:ListNode?, _ x:Int)->ListNode?{
letleftDummy=ListNode(0)
letrightDummy=ListNode(0)
varleft= leftDummy
varright= rightDummy
varhead= head
whilelet current = head {
if current.val < x {
left.next = current
left = left.next!
}else{
right.next = current
right = right.next!
}
head = head?.next
}
right.next =nil
left.next = rightDummy.next
return leftDummy.next
}
}